﻿using Shop.RemoteModel.Shop;
using Shop.RemoteModel.Shop.Model;
using Shop.StoreGatewayModular.Interface;
using Shop.StoreGatewayModular.Linq;
using WeDonekRpc.Helper;
using WeDonekRpc.Modular;

namespace Shop.StoreGatewayModular.Service
{
    internal class ShopService : IShopService
    {
        public long Login (IUserState user, long companyId)
        {
            long shopId = user.ToShopId();
            if (shopId == 0)
            {
                shopId = new ShopLogin
                {
                    CompanyId = companyId
                }.Send();
                user["ShopId"] = shopId;
                _ = user.SaveState();
            }
            return shopId;
        }
        public bool Set (IUserState user, ShopSetArg datum)
        {
            long shopId = user.ToShopId();
            if (shopId == 0)
            {
                throw new ErrorException("shop.no.login");
            }
            return new SetShop
            {
                Id = shopId,
                Datum = datum
            }.Send();
        }
        public ShopDatum Get (IUserState user)
        {
            long shopId = user.ToShopId();
            if (shopId == 0)
            {
                throw new ErrorException("shop.no.login");
            }
            return new GetShop
            {
                Id = shopId
            }.Send();
        }
    }
}
